package com.sun;

import java.util.*;

/**
 * https://leetcode.cn/problems/3sum/
 * 给你一个整数数组 nums ，判断是否存在三元组 [nums[i], nums[j], nums[k]]
 *      满足 i != j、i != k 且 j != k ，同时还满足 nums[i] + nums[j] + nums[k] == 0 ;
 *
 */

public class Test15三数之和 {
    public static void main(String[] args) {
        int[] nums = {-1,0,1,2,-1,-4};
        int[] nums1 = {0};
        List<List<Integer>> lists = threeSum2(nums);
        lists.forEach(System.out::println);
    }

    /**
     *方法1：自己，暴力循环
     */
    public static List<List<Integer>> threeSum1(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if(nums == null || nums.length < 3){
            return res;
        }
        boolean c = true;
        for (int i = 0; i < nums.length - 2; i++) {
            for (int j = i + 1; j < nums.length -1; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    if (nums[i] + nums[j] + nums[k] == 0){
                        List<Integer> temp = new ArrayList<>(){};
                        temp.add(nums[i]);
                        temp.add(nums[j]);
                        temp.add(nums[k]);
                        if (nums[i] == nums[j] && nums[j] ==  nums[k] && c){
                            c = false;
                            res.add(temp);
                            continue;
                        }
                        boolean b = true;
                        for (List<Integer> re : res) {
                            if (re.contains(nums[i]) && re.contains(nums[j])
                                    && re.contains(nums[k])){
                                b = false;
                            }
                        }
                        if (b){
                            res.add(temp);
                        }

                    }
                }
            }
        }
        return res;
    }

    /**
     * 方法2：力扣官方
     */

    public static List<List<Integer>> threeSum2(int[] nums) {
        int n = nums.length;
        Arrays.sort(nums);
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        // 枚举 a
        for (int first = 0; first < n; ++first) {
            // 需要和上一次枚举的数不相同
            if (first > 0 && nums[first] == nums[first - 1]) {
                continue;
            }
            // c 对应的指针初始指向数组的最右端
            int third = n - 1;
            int target = -nums[first];
            // 枚举 b
            for (int second = first + 1; second < n; ++second) {
                // 需要和上一次枚举的数不相同
                if (second > first + 1 && nums[second] == nums[second - 1]) {
                    continue;
                }
                // 需要保证 b 的指针在 c 的指针的左侧
                while (second < third && nums[second] + nums[third] > target) {
                    --third;
                }
                // 如果指针重合，随着 b 后续的增加
                // 就不会有满足 a+b+c=0 并且 b<c 的 c 了，可以退出循环
                if (second == third) {
                    break;
                }
                if (nums[second] + nums[third] == target) {
                    List<Integer> list = new ArrayList<Integer>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    ans.add(list);
                }
            }
        }
        return ans;
    }

    /**
     * 方法3：自己默写方法2
     */
    public static List<List<Integer>> threeSum3(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
//        Collections.sort();
        Arrays.sort(nums);
        int n = nums.length;
        for(int first = 0; first < n; first++){
            if (first > 0 && nums[first] == nums[first -1]){
                continue;
            }
            int target = -nums[first];
            int third = n - 1;
            for(int second = first + 1; second < n; second++){
                if(second > first + 1 && nums[second] == nums[second-1]){
                    continue;
                }
                while (second < third && nums[second] + nums[third] > target){
                    third--;
                }
                if (second == third){
                    break;
                }
                if(nums[second] + nums[third] == target){
                    ArrayList<Integer> integers = new ArrayList<>();
                    integers.add(nums[first]);
                    integers.add(nums[second]);
                    integers.add(nums[third]);
                    res.add(integers);
                }
            }
        }
        return res;
    }

}
